<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/9
 * Time: 16:46
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class NoticeModel
{
    const TABLE = 'notice';

    /**
     * 首页公告列表
     * @param $where
     * @return mixed
     */
    public function showLists($where){
        return DB::table(self::TABLE)
            ->where(function ($query) use ($where){
                $query->where($where)->orWhere('projectId', '0');
            })
            ->where('isShow',1)
            ->orderBy('isTop', 'desc')
            ->orderBy('updateTime' ,'desc')
            ->limit(10)
            ->get()->toArray();
    }

    /**
     * 通知详情
     * @param array $where
     * @return array
     */
    public function info(array $where)
    {
        $result = DB::table(self::TABLE)->where($where)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * 通知列表
     * @param array $param
     * @return mixed
     */
    public function lists(array $param)
    {
        $limit = config('yucheng.limit');
        $start = 0;
        if (!empty($param['length'])) {
            $limit = $param['length'];
        }
        if (!empty($param['page'])) {
            $start = ($param['page']-1)*$limit;
        }
        return DB::table(self::TABLE)
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->where(function($query) use ($param){
                if (!empty($param['projectId'])){
                    $query->where(self::TABLE.'.projectId',$param['projectId'])->orWhere('projectId', '0');
                }
            })
            ->orderBy('isTop', 'desc')
            ->orderBy('updateTime','desc')
            ->offset($start)->limit($limit)
            ->select(self::TABLE.'.*','p.name as projectName')
            ->get()->toArray();
    }

    /**
     * @return mixed
     */
    public function countLists()
    {
        return DB::table(self::TABLE)->count();
    }

    /**
     * 添加公告
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * 更新公告
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * 删除公告
     * @param array $where
     * @return mixed
     */
    public function delete(array $where)
    {
        return DB::table(self::TABLE)->where($where)->delete();
    }
}